package GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.TreeMap;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import msgParser.Message;
import msgParser.Parser;
import msgParser.Strings;
import msgParser.Validation;

import Core.Connection;
import Core.Rank;
import Core.SessionData;

public class CreatePostMenu extends AbstractMenu 
{
	private JLabel label;
	private JButton disconnectButton;
	private JLabel userMode;
	private JButton forumButton;
	private JButton subForumButton;
	private JButton topicButton;
	private JButton logoutButton;
	private JTextArea replyText;
	private JButton createPostButton;
	private JScrollPane replyTextScroll;
	private JLabel postReply;
	
	public CreatePostMenu(JFrame frame) 
	{
		super(frame);
		
		label = new JLabel("Post a Reply");
		label.setBounds(300,30,300,30);
		label.setFont(new Font(label.getFont().getName(),label.getFont().getStyle(), 30));
		
		postReply = new JLabel("Enter your reply:");
		postReply.setBounds(300,175,300,30);
		postReply.setFont(new Font(postReply.getFont().getName(),postReply.getFont().getStyle(), 15));
		
		userMode = new JLabel(MainWindow.data.rank.toString() + " mode");
		userMode.setBounds(10,500,200,30);
		userMode.setFont(new Font(userMode.getFont().getName(),userMode.getFont().getStyle(), 15));
		userMode.setForeground(new Color(0, 0, 255));
		
		replyText = new JTextArea();
		replyText.setBorder(new LineBorder(null));
		replyText.setBounds(300, 200, 450, 270);
		replyText.setFont(new Font(replyText.getFont().getName(),replyText.getFont().getStyle(), 13));
		replyText.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		replyTextScroll = new JScrollPane(replyText, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		replyTextScroll.setPreferredSize(new Dimension(300, 100));
		replyTextScroll.setBounds(300, 200, 450, 270);
		
		forumButton = new JButton("Back to Forum");
		forumButton.setBounds(25, 230, 130, 20);
		forumButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent e)
            {
            	MainWindow.switchMenu(new ForumMenu(getFrame()));
            }
		});
		
		topicButton = new JButton("Back to Topic");
		topicButton.setBounds(25, 200, 130, 20);
		topicButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent e)
            {
            	MainWindow.switchMenu(new TopicMenu(getFrame()));
            }
		});
		
		subForumButton = new JButton("Back to " + MainWindow.data.currentSubForum);
		subForumButton.setBounds(25, 260, 130, 20);
		subForumButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent e)
            {
            	MainWindow.switchMenu(new SubForumMenu(getFrame()));
            }
		});
		
		disconnectButton = new JButton(new ImageIcon("disconnect.PNG"));
		disconnectButton.setBounds(10, 530, 32, 32);
		disconnectButton.setContentAreaFilled(false);
		disconnectButton.setBorder(null);
		disconnectButton.addMouseListener(new MouseAdapter() 
		{
		    public void mousePressed(MouseEvent evt) 
		    {
		    	disconnectButton.setIcon(new ImageIcon("disconnect2.PNG"));
            	disconnectButton.repaint();
		    }
		    public void mouseReleased(MouseEvent evt) 
		    {
		    	Point p = evt.getPoint();
		    	disconnectButton.setIcon(new ImageIcon("disconnect.PNG"));
            	disconnectButton.repaint();
		    	if(p.x < 0 || p.y < 0 || p.x > disconnectButton.getWidth() || p.y > disconnectButton.getHeight()) return;
            	Connection.disconnect();
            	MainWindow.data = new SessionData();
            	MainWindow.switchMenu(new ConnectMenu(getFrame()));
		    }
		});
		
		logoutButton = new JButton("Log out");
		logoutButton.setBounds(25, 140, 100, 20);
		logoutButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent e)
            {
            	Connection.send(Message.createMessage(Strings.LOGOUT));
            }
		});
		
		createPostButton= new JButton("Create");
		createPostButton.setBounds(300, 480, 100, 30);
		createPostButton.setFont(new Font(createPostButton.getFont().getName(),Font.BOLD, 15));
		createPostButton.setForeground(new Color(255, 0, 0));
		createPostButton.addActionListener(new ActionListener() 
		{
            public void actionPerformed(ActionEvent e)
            {
            	String text = replyText.getText();
            	if(!Validation.validateString(text, Validation.HAS_LETTERS))
            	{
            		JOptionPane.showMessageDialog(null, "Reply content cannot be empty.", "Error", JOptionPane.ERROR_MESSAGE);
            		return;
            	}
            	
            	Connection.send(Message.createMessage(Strings.ADD_REPLY, text, ""+MainWindow.data.topics.get(MainWindow.data.currentTopic)));
            }
		});
	}

	public void connectionInterrupted() 
	{
		JOptionPane.showMessageDialog(null, "Connection has crashed. Please reconnect...", "Connection Error", JOptionPane.ERROR_MESSAGE);
		MainWindow.switchMenu(new ConnectMenu(getFrame()));
	}

	public void incomingMessage(String msg) 
	{
		Parser p = new Parser(msg);
		p.Parse();
		try 
		{
			
			if(p.testHeader(Strings.LOGGED_OUT))
			{
				MainWindow.data.loggedIn = false;
				MainWindow.data.rank = Rank.GUEST;
				JOptionPane.showMessageDialog(null, "You have logged out of the system.", "Logged out", JOptionPane.INFORMATION_MESSAGE);
				MainWindow.switchMenu(new LobbyMenu(getFrame()));
			}
			else if(p.testHeader(Strings.ERROR_LOG_OUT))
			{
				JOptionPane.showMessageDialog(null, "Logout error: " + p.argStr(1), "Unexpected error", JOptionPane.ERROR_MESSAGE);
			}
			else if(p.testHeader(Strings.REPLY_ADDED))
			{
				Object[] options = {"Back to " + MainWindow.data.currentSubForum, "Back to thread"};
				int n = JOptionPane.showOptionDialog(null,
				"Your reply was posted.",
				"Success",
				JOptionPane.YES_NO_OPTION,
				JOptionPane.INFORMATION_MESSAGE,
				null,     
				options,  
				options[0]); 
				if(n == JOptionPane.YES_OPTION)
				{
					MainWindow.switchMenu(new SubForumMenu(getFrame()));
				}
				else
				{
					MainWindow.switchMenu(new TopicMenu(getFrame()));
				}
			}
			else if(p.testHeader(Strings.ERROR_ADD_REPLY))
			{
				JOptionPane.showMessageDialog(null, "Error: " + p.argStr(1), "Error", JOptionPane.ERROR_MESSAGE);
			}
		} 
		catch (Exception e) {}
	}

	public void create() 
	{
		regItem(label);
		regItem(logoutButton);
		regItem(disconnectButton);
		regItem(userMode);
		regItem(forumButton);
		regItem(subForumButton);
		regItem(topicButton);
		regItem(createPostButton);
		regItem(postReply);
		regItem(replyTextScroll);
		getFrame().repaint();
	}

}
